/*
题目链接:https://leetcode.cn/problems/count-subarrays-where-max-element-appears-at-least-k-times/description/?envType=daily-question&envId=2025-04-29
*/

//题解代码:

class Solution {
public:
    #define ll long long
    long long countSubarrays(vector<int>& nums, int k) {
        int n = nums.size();
        ll ans = 0;
        for(int l=0,r=0,mx=0,cnt=0;r<n;++r){
            if(nums[r] > mx){
                mx = nums[r];
                cnt = 1;
                ans = 0;
            }else if(nums[r] == mx){
                ++cnt;
            }
            while(cnt>=k){
                if(nums[l]==mx){
                    if(cnt>k) --cnt;
                    else break;
                }
                ++l;
            }
            if(cnt==k) ans += l+1;
        }
        return ans;
    }
};
